<template>
  <div>
    <el-tag
      v-for="(item, index) in tagList"
      :key="index"
      closable
      @close="delTag(item)"
    >
      {{ item }}
    </el-tag>
    <el-input
      class="input-new-tag"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="addTag(inputValue),inputValue='',inputVisible= false"
      @blur="inputBlur"
      @keydown.esc.native="valueClear"
    >
    </el-input>
    <el-button v-else class="button-new-tag" size="small" @click="showInput"
      >+ New Tag</el-button
    >
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex'

export default {
  data () {
    return {
      inputVisible: false,
      inputValue: ''
    }
  },
  computed: {
    ...mapState(['tagList'])
  },
  methods: {
    ...mapMutations(['delTag', 'addTag']),
    showInput () {
      this.inputVisible = true
      this.$nextTick(_ => {
        // console.log(this.$refs.saveTagInput.$refs.input)
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    valueClear () {
      this.inputValue = ''
    },
    inputBlur () {
      this.inputValue = ''
      this.inputVisible = false
    }
  }
}
</script>
<style>
 .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px !important;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px !important;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
